<?php


if (!function_exists('datatableResponse')) {
    function datatableResponse($draw, $total, $data = [])
    {
        return response()->json([
            'draw' => $draw,
            'recordsFiltered' => $total,
            'recordsTotal' => $total,
            'data' => $data
        ]);
    }
}



if (!function_exists('escapeLikeStr')) {
    function escapeLikeStr($str)
    {
        $like_escape_char = '\\';
        $str = str_replace([$like_escape_char, '%', '_', '"'], [
            $like_escape_char . $like_escape_char,
            $like_escape_char . '%',
            $like_escape_char . '_',
            $like_escape_char . '"',
        ], $str);

        // 过滤表情
        return getValueFilter($str);
    }
}


if (!function_exists('getValueFilter')) {

    function getValueFilter(string $value)
    {
        //执行一个正则表达式搜索并且使用一个回调进行替换 过滤表情
        return trim(preg_replace_callback(
            '/./u',
            function (array $match) {
                return strlen($match[0]) >= 4 ? '' : $match[0];
            },
            $value), ' ');
    }
}
